<template>
  <div class="shop-cart-all">
    <div v-if="$root.$data.cart != null && $root.$data.cart.length !== 0">
      <div class="shop-cart-title">
        <div style="width: 80px">商品图片</div>
        <div style="width: 260px">商品描述</div>
        <div>商品价格</div>
        <div>商品数量</div>
        <div>操作</div>
      </div>
      <shop-cart-item
        v-for="item in $root.$data.cart"
        :key="item.id"
        :img-height="80"
        :font-size="16"
        :pdescw="260"
        :data="item"
      ></shop-cart-item>
      <div class="settlement">
        <div>商品总价:</div>
        <div style="color: red">￥{{getSum()}}</div>
        <div>商品数量:</div>
        <div style="color: red">{{getProductNum()}}件</div>
        <el-button type="primary" @click="settlement" icon="el-icon-sold-out">结算</el-button>
      </div>
    </div>
    <div class="null" v-else>购物车没有任何商品</div>
  </div>
</template>

<script>
import shopCartItem from "../components/shopCartItem";

export default {
  name: "ShopCart",
  methods: {
    getSum() {
      let sum = 0;
      this.$root.$data.cart.forEach(v => {
        sum += v.num * (v.product.price * 1000);
      });
      return sum / 1000;
    },
    getProductNum() {
      let sum = 0;
      this.$root.$data.cart.forEach(v => {
        sum += v.num;
      });
      return sum;
    },
    settlement() {
      this.$router.push({ path: "/Settlement" });
    }
  },
  components: {
    shopCartItem: shopCartItem
  }
};
</script>

<style scoped lang="scss">
.shop-cart-title {
  display: flex;
  justify-content: space-evenly;
  margin-bottom: 20px;
  > * {
    margin-right: 20px;
    text-align: center;
  }
}

.settlement {
  padding: 20px 0;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  > * {
    margin-left: 20px;
  }
}

.shop-cart-all {
  min-height: 78vh;
}

.null {
  font-size: 30px;
  color: #888;
  text-align: center;
  margin-top: 100px;
}
</style>